/***************************************************************************
 创建者: 华磊
 开始时间: 2020.8.14
 copyright: (C) 深圳市华友高科有限公司
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef MAPSCENE_H
#define MAPSCENE_H
#include <QGraphicsScene>
#include <QStyleOptionGraphicsItem>
#include "bezier.h"
#ifdef BUILD_X86_LINUX
#include "../CommunicateDelegate/gSoapFoundation/soapStub.h"
#endif

class BaseMapImage;

class MapLogic;
/**
 * @brief The MapScene class
 */
class MapScene : public QGraphicsScene
{
    Q_OBJECT
public:
    explicit MapScene(MapLogic *mapLogicIn,QObject *parent = 0);

public:
    int setShowCommonCar(bool isShow);
    int setShowCommonPositionCertainty(bool isShow);
    int setShowReflectCar(bool isShow);
    int setShowReflectPositionCertainty(bool isShow);
    int reloadMap(MapPropertyInfo infoIn);
    int getScenePosition(double &x1,double &y1,double &x2,double &y2);
    int getWorldPosition(double &x, double &y, double &a);
    double getWorldLength();
    int setCarBorderRect(double x, double y, double width,double height);

    int drawTargetPoint(bool isShow, double x_pix, double y_pix, double a_degree);// pix, pix, degree
    int drawCommonCar(double x_pix, double y_pix, double a_degree);// pix, pix, degree
    int drawCommonPositionCertainty(double x, double y,double positionError);
    int drawReflectCar(double x_pix, double y_pix, double a_degree);// pix, pix, degree
    int drawReflectPositionCertainty(double x, double y,double positionError);
    int drawVmark(bool isShow, double x_pix, double y_pix, double a_degree);// pix, pix, degree
    int drawLaserPoint(std::vector<struct PointCloudData_3D> &pointCloudOut,double x,double y,double a);
    int drawPathPoint(const std::vector<POINT> &pointList);
    int hideLaserPoint();

    /********************************************************
     * 修改人:       hualei
     * 修改时间:   2018-12-11
     * 修改说明: 在地图上绘制目标点位
     * ******************************************************/
    /**
     * @brief drawPointList             在地图上绘制目标点位
     * @param isShow                是否显示
     * @param pos_X_Pix             绘制点位的显示坐标X
     * @param pos_Y_Pix             绘制点位的显示坐标Y
     * @param pos_A_Degree          绘制点位的显示角度
     * @return                      绘制成功：1  失败:0
     */
    int drawVehiclePointList(bool isShow, double pos_X_Pix, double pos_Y_Pix, double pos_A_Degree, int pointIndex);
    int freeDPointListItem();
    int drawOneBezier(VehiclePosition startPosition, VehiclePosition targetPosition, double lengthRatio, bool isPositiveRun);
    int drawOneBezierByControlPoints(VehiclePosition startPosition, VehiclePosition targetPosition,
                                     VehiclePosition middlePose1,
                       VehiclePosition middlePose2);
    int calculateBezierPoints(VehiclePosition startPosition, VehiclePosition targetPosition, double lengthRatio,
                              bool isPositiveRun,std::vector<POINT> &pointsOut_pix);
    int calculateBezierPointsByControlPoints(VehiclePosition startPosition, VehiclePosition targetPosition,
                                             VehiclePosition middlePose1,
                               VehiclePosition middlePose2, std::vector<POINT> &pointsOut_pix);
    int drawContinuousBezier(VehiclePosition startPosition, std::vector<VehiclePosition> targetPosition, double lengthRatio, bool isPositiveRun);
    int drawCurrentPath_line(VehiclePosition startPosition,VehiclePosition targetPosition);
    int drawCurrentPath_rotate(VehiclePosition targetPosition);

    int displayAllReferenceMarks(const std::vector<ReflectMarkInfo> &allReflectMarksIn);
    int freeAllReferenceMarks();
    int displayCalculatedReferenceMarks(const std::vector<ReflectMarkInfo> &marksIn);
    int freeCalculatedReferenceMarks();
    int displayDetectedLaserPoints(std::vector<struct LaserMarkInfo> laserMarks);
    int freeDetectedLaserPoints();

protected:


signals:

public slots:

protected:
    void keyPressEvent(QKeyEvent *event);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);


private:

    /**
     * @brief drawTmpPosition       绘制地图点位置
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    void drawUserClickedPoint(double x1,double y1,double x2,double y2);

    /**
     * @brief initialCar            初始化AGV小车图画
     * @return                      1:Success
     */
    int initialCommonCar();
    int initialReflectCar();

    /**
     * @brief initialTargetPoint    初始化目标点
     * @return
     */
    int initialTargetPoint();

    /**
     * @brief initialVmark          初始化VMark槽标识
     * @return
     */
    int initialVmark();

    /**
     * @brief initialLaserPoint     初始化激光点
     * @return                      1:Success
     */
    int initialLaserPoint();
    int initialPathPoint();

    /**
     * @brief initialPoint          初始化绘制点位(图形的具体实现)
     * @return
     */
    int initialPoint();

signals:
    void updatePointsSignal();
    void userPointClicked_signal(double x,double y,double r);

private:

    /**
     * @brief pressed_x             鼠标按下时点位X轴坐标
     */
    double pressed_x;

    /**
     * @brief pressed_y             鼠标按下时点位Y轴坐标
     */
    double pressed_y;

    /**
     * @brief released_x            鼠标释放时点位X轴坐标
     */
    double released_x;

    /**
     * @brief released_y            鼠标释放时点位Y轴坐标
     */
    double released_y;

    double car_positionX_P;//PIX
    double car_positionY_P;//PIX
    double car_positionA_P;//degree

    MapLogic *mapLogic;

    BaseMapImage *baseMap;
    QGraphicsLineItem *userPoint_line;
    QGraphicsEllipseItem *userPoint_circle;
    QGraphicsTextItem *userPoint_text;
    //目标牵引线
    QGraphicsLineItem *target_position_line;
    QGraphicsLineItem *target_position_connect_line;
    QGraphicsEllipseItem *target_position_circle;
    QGraphicsTextItem *target_position_text;

    //common car
    QGraphicsTextItem *car_text;
    QGraphicsRectItem *carBorder;
    QGraphicsEllipseItem *car_positionCertainCircle;
    bool isShowCommonCar;
    bool isShowCommonCertainCircle;
    //reflect car
    QGraphicsTextItem *reflectcar_text;
    QGraphicsRectItem *reflectcarBorder;
    QGraphicsEllipseItem *reflectcar_positionCertainCircle;
    bool isShowReflectCar;
    bool isShowReflectCertainCircle;

    QGraphicsTextItem *vmark_text;

    QGraphicsPolygonItem *laserPointPolygon;
    QGraphicsPolygonItem *currentMovingPath_spline;
    QGraphicsLineItem *currentMovingPath_line;
    QGraphicsTextItem *currentMovingPath_rotate;

    /**
     * @brief target_Point      绘制目标点位对象
     */
    QGraphicsEllipseItem *target_Point;

    QGraphicsPixmapItem *tmp_target_Point;

    /**
     * @brief targetPointText   目标点号
     */
    QGraphicsTextItem *tmp_targetPointText;

    /**
     * @brief carPixMap     显示小车图片
     */
    QPixmap *carPixMap;

    /**
     * @brief pointPixMap   显示点位图片
     */
    QPixmap *pointPixMap;

    /**
     * @brief tmpPointList      保存地图目标点对象
     */
    QVector <QGraphicsPixmapItem *> vehiclePointList;

    /**
     * @brief tmpPointTextList  保存地图点索引值对象
     */
    QVector <QGraphicsTextItem *> vehiclePointTextList;

    QVector <QGraphicsEllipseItem*> allReferenceMarksList_dot;
    QVector <QGraphicsTextItem*> allReferenceMarksList_text;

    QVector <QGraphicsEllipseItem*> calculatedMarksList_dot;
    QVector <QGraphicsTextItem*> calculatedMarksList_text;

    QVector <QGraphicsEllipseItem*> detectedLaserList_dot;
    QVector <QGraphicsTextItem*> detectedLaserList_text;

};

#endif // MAPSCENE_H
